const app = getApp()
import {urls} from '../../utils/urls'
let _this;
Page({
  data: {
    banner_list: [],
    isFirst: true,
    service: [],
    memr: '',
    notified : false
  },
  navToArea() {
    wx.navigateTo({
      url: '/pages/area/area',
    })
  },
  navTo(e) {
    if (wx.getStorageSync("user") == '' || wx.getStorageSync("user").phone == null || wx.getStorageSync("user") == undefined) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } else {
      let name = e.currentTarget.dataset.name
      let index = e.currentTarget.dataset.index
      // console.log(name,index);
      if (this.data.service[index].is_show == 1) {
        if (name == '快递代取') {
          wx.navigateTo({
            url: '/pages/courier/courier',
          })
        } else if (name == '校园跑腿') {
          wx.switchTab({
            url: '/pages/help/help',
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '服务暂停中',
          showCancel: false,
          confirmText: '朕知道了',
          confirmColor: '#6887e1'
        })
      }
    }
  },
  onLoad() {
    _this = this
    if (_this.data.isFirst) {
      _this.checkArea()
      _this.setData({
        isFirst: false
      })
    }
  },
  getCarousel() {
    this.setData({
      banner_list: wx.getStorageSync('school').banner
    })
  },
  onPullDownRefresh() {
    _this.checkArea()
    _this.getCarousel()
  },
  getService() {
    this.setData({
      server: wx.getStorageSync('server')
    })
  },
  checkArea() {
    if (wx.getStorageSync('school')) {
      this.setData({
        school: wx.getStorageSync('school'),
        service: wx.getStorageSync('service'),
      })
    } else {
      console.log();
      wx.navigateTo({
        url: '/pages/area/area',
      })
    }
  },
  getMemr(callback) {
    app.http('GET', urls.memr, { 'schoolid': wx.getStorageSync('school').id },function (res) {
      if (res.code == 100) {
        _this.setData({
          memr: res.results
        })
        if (typeof callback === 'function') {
          callback()
        }
      }
    })
  },
  onShow() {
    this.checkArea()
    this.getCarousel()
    if (wx.getStorageSync('user')){
      this.getMemr(()=>{
        if (!this.data.notified) {
          wx.showModal({
            title: this.data.memr[0].title,
            content: this.data.memr[0].content,
            cancelText: '取消',
            confirmText:'确认',
            confirmColor:'#6887e1',
            complete: (res) => {
              if (res.cancel) {
              _this.setData({
                notified : true
              })
              }      
              if (res.confirm) {
                _this.setData({
                  notified : true
                })
              }
            }
          })
        }
      }
    )
    }

  },
})